package com.heba.helper;

import android.content.Context;

import com.heba.ATApp;
import com.heba.Debug;
import com.heba.helper.callback.FileUploadListener;
import com.tencent.cos.xml.CosXmlServiceConfig;
import com.tencent.cos.xml.CosXmlSimpleService;
import com.tencent.cos.xml.exception.CosXmlClientException;
import com.tencent.cos.xml.exception.CosXmlServiceException;
import com.tencent.cos.xml.listener.CosXmlProgressListener;
import com.tencent.cos.xml.listener.CosXmlResultListener;
import com.tencent.cos.xml.model.CosXmlRequest;
import com.tencent.cos.xml.model.CosXmlResult;
import com.tencent.cos.xml.transfer.COSXMLUploadTask;
import com.tencent.cos.xml.transfer.TransferConfig;
import com.tencent.cos.xml.transfer.TransferManager;
import com.tencent.cos.xml.transfer.TransferState;
import com.tencent.cos.xml.transfer.TransferStateListener;
import com.tencent.qcloud.core.auth.QCloudCredentialProvider;
import com.tencent.qcloud.core.auth.ShortTimeCredentialProvider;

/**
 * 腾讯云图片上传
 * Created by jason on 2019/7/3.
 */
public class FileUploadHelper {

    /**
     * 腾讯云文件存储对象
     */

    private CosXmlServiceConfig serviceConfig;
    private CosXmlSimpleService cosXmlSimpleService;
    private String appid = "1253933048";
    private String region = "ap-guangzhou";

    public FileUploadHelper(Context context) {
        initCosClient(context);
    }

    /**
     * 初始化cosClient
     */
    private void initCosClient(Context context) {
        //初始化 cosClient
        serviceConfig = new CosXmlServiceConfig.Builder().setAppidAndRegion(appid, region).setDebuggable(true).builder();
        /**
         * 初始化对象，来给SDK提供临时密钥
         */
        QCloudCredentialProvider credentialProvider = new ShortTimeCredentialProvider(ATApp.CosSecretId,
                ATApp.CosSecretKey, 300);
        cosXmlSimpleService = new CosXmlSimpleService(context, serviceConfig, credentialProvider);
    }

    /**
     * 上传文件
     *
     * @param srcPath 本地图片路径
     * @param cosPath 上传成功图片名字
     */
    public void upload(String srcPath, String cosPath, FileUploadListener uploadListener) {
        // 初始化 TransferConfig
        TransferConfig transferConfig = new TransferConfig.Builder().build();
        TransferManager transferManager = new TransferManager(cosXmlSimpleService, transferConfig);

        String bucket = "greenmulu";
        String uploadId = null;//用于续传，若无，则为null.
        //上传文件
        COSXMLUploadTask cosxmlUploadTask = transferManager.upload(bucket, cosPath, srcPath, uploadId);
        //设置上传进度回调
        cosxmlUploadTask.setCosXmlProgressListener(new CosXmlProgressListener() {
            @Override
            public void onProgress(long complete, long target) {
                float progress = 1.0f * complete / target * 100;
                Debug.Log("upload onProgress", String.format("progress = %d%%", (int) progress));
            }
        });
        //设置返回结果回调
        cosxmlUploadTask.setCosXmlResultListener(new CosXmlResultListener() {
            @Override
            public void onSuccess(CosXmlRequest request, CosXmlResult result) {
                String imgUrl = "";
                if (!result.accessUrl.startsWith("http://") && !result.accessUrl.startsWith("https://")) {
                    imgUrl = "http://" + result.accessUrl;
                } else {
                    imgUrl = result.accessUrl;
                }
                Debug.Log("upload success", "imgUrl = " + imgUrl);

                //回調接口
                uploadListener.onSuccess(imgUrl);
            }

            @Override
            public void onFail(CosXmlRequest request, CosXmlClientException exception, CosXmlServiceException serviceException) {
                Debug.Log("upload", "Failed: " + (exception == null ? serviceException.getMessage() : exception.toString()));
                //回調接口
                uploadListener.onFail();
            }
        });
        //设置任务状态回调, 可以查看任务过程
        cosxmlUploadTask.setTransferStateListener(new TransferStateListener() {
            @Override
            public void onStateChanged(TransferState state) {
                Debug.Log("upload", "Task state:" + state.name());
            }
        });
    }

}
